package com.hk.webapp.controller.admin;

import com.hk.webapp.bean.SysUserArticleCate;
import com.hk.webapp.service.SysUserArticleCateService;
import com.hk.webapp.vo.IdVo;
import com.hk.webapp.vo.ResultVo;
import com.hk.webapp.vo.SetArticleActionVo;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
public class SysUserArticleCateController extends AdminBaseController {
    @Resource
    private SysUserArticleCateService sysUserArticleCateService;

    @PostMapping("/SysUserArticleCate/getArticleActionBySysUserId")
    public ResultVo getArticleActionBySysUserId(@RequestBody IdVo vo){
        if (vo.getId()<=0){
            return ResultVo.createByError("改管理员并不存在");
        }
        SysUserArticleCate sysUserArticleCate=sysUserArticleCateService.selectBySysUserId(vo.getId());
        if (sysUserArticleCate==null){
            return ResultVo.createByError("改管理员并不存在");
        }
        return ResultVo.createBySuccessData(sysUserArticleCate);
    }

    @PostMapping("/SysUserArticleCate/setArticleAction")
    public ResultVo setArticleAction(@ApiParam(hidden = true) @RequestHeader(value = "userId") Integer userId, @RequestBody SetArticleActionVo vo){
        if (vo.getId()<=0){
            return ResultVo.createByError("请选择要设置的项");
        }
        SysUserArticleCate sysUserArticleCate= sysUserArticleCateService.selectBySysUserId(vo.getId());
        if (sysUserArticleCate==null){
            sysUserArticleCate=new SysUserArticleCate();
            sysUserArticleCate.setArticleIds(StringUtils.join(vo.getIds(),","));
            sysUserArticleCate.setCreateTime(new Date());
            sysUserArticleCate.setIsDeleted(0);
            sysUserArticleCate.setSysUserId(vo.getId());
            sysUserArticleCateService.insert(sysUserArticleCate);
            if (sysUserArticleCate.getId()<=0){
                return ResultVo.createByError("设置失败");
            }
        }else {
            if(sysUserArticleCateService.updateIdsById(sysUserArticleCate.getId(),StringUtils.join(vo.getIds(),","))<=0){
                return ResultVo.createByError("设置失败");
            }
        }
        return ResultVo.createBySuccessData("succeed");
    }
}
